package com.admin.system.controller;

import com.admin.base.BaseController;
import com.admin.base.Page;
import com.admin.base.Response;
import com.admin.system.entity.*;
import com.admin.system.service.SystemGroupService;
import com.admin.system.service.SystemLogService;
import com.admin.system.service.SystemUserService;
import com.admin.system.vo.query.SystemLogQuery;
import com.admin.system.vo.query.SystemUserQuery;
import com.admin.utils.SnCal;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : alex
 * @date : 2020/7/4
 */
@Controller
@RequestMapping(value = "/externalAssembly")
public class ExternalAssemblyController extends BaseController {
    @Value("${baidu.map.ak}")
    private String mapAk;
    @Value("${baidu.map.url}")
    private String mapUrl;

    @Autowired
    SystemLogService systemLogService;

    /**
     * 列表页面
     */
    @RequestMapping(value = "/echarts")
    public ModelAndView echarts(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("system/externalAssembly/echarts");
        return mav;
    }

    @RequestMapping(value = "/echarts/systemLog")
    @ResponseBody
    public Response echartsSystemLog(HttpServletRequest request, HttpServletResponse response, SystemLogQuery query) {
        List<SystemLog> systemLogs = systemLogService.listByConditions(query);
        //一整年的访问总数统计
        LocalDate now = LocalDate.now().plusMonths(-11);
        Map<String, ChartShow> chartShowMap = new HashMap<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        List<ChartShow> chartShows = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            ChartShow chartShow = new ChartShow();
            String name = now.format(formatter);
            chartShow.setName(name);
            chartShow.setValue(new BigDecimal(0));
            for (SystemLog systemLog : systemLogs) {
                String createTime = systemLog.getCreateTime().format(formatter);
                if (name.equals(createTime)) {
                    chartShow.setValue(chartShow.getValue().add(new BigDecimal(1)));
                }
            }
            now = now.plusMonths(1);
            chartShows.add(chartShow);
        }
        return Response.ok(chartShows);
    }

    /**
     * 百度地图
     */
    @RequestMapping(value = "/map")
    public ModelAndView map(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {

        mav.setViewName("system/externalAssembly/map");
        String srcMapUrl = mapUrl + "&ak=" + mapAk;
        mav.addObject("srcMapUrl", srcMapUrl);
        return mav;
    }

    /**
     * 加载地图上的点
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/map/markerPoint")
    @ResponseBody
    public Response mapMarkerPoint(HttpServletRequest request, HttpServletResponse response) {
        MapPoint laomengdong = new MapPoint("118.805297", "32.018373", "老门东");
        MapPoint dongnandaxue = new MapPoint("118.827718", "31.886509", "东南大学");
        MapPoint nanjingdaxue = new MapPoint("118.962249", "32.127054", "南京大学");
        List<MapPoint> list = new ArrayList<>();
        list.add(laomengdong);
        list.add(dongnandaxue);
        list.add(nanjingdaxue);
        return Response.ok(list);
    }
}
